'use strict';

/**
 * model.js
 * instead of init the model inside the index.js file
 * we could take care of that here. Then just export a db instance back.
 * This way we further abstract away the tie between app and database backend
 */

var config = require('./config'),
    umzug = require('umzug'), // https://github.com/sequelize/umzug <-- for migration [TODO]
    chalk = require('chalk'),
	mongoose = require('mongoose'),
	db = module.exports;

// init db 
db = mongoose.connect(config.db.uri, config.db.options, function(err) {
	if (err) {
		console.error(chalk.red('Could not connect to MongoDB!'));
		console.log(chalk.red(err));
	}
});
/**
 * listening to error, is this a double call? see above line
 */
mongoose.connection.on('error', function(err)
{
	console.error(chalk.red('MongoDB connection error: ' + err));
	process.exit(-1);
});
